package com.cheyunkeji.er.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Base64;

import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.bean.evaluate.CarBasicInfoList;
import com.cheyunkeji.er.bean.evaluate.CarTempAttrList;
import com.cheyunkeji.er.bean.evaluate.EvaluateDetailResult2;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by Michael on 2017/4/21 20:30.
 */

public class SerializeUtil {

    /**
     * writeObject 方法负责写入特定类的对象的状态，以便相应的 readObject 方法可以还原它
     * 最后，用Base64.encode将字节文件转换成Base64编码保存在String中
     *
     * @param object 待加密的转换为String的对象
     * @return String   加密后的String
     */
    private static String Object2String(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            objectOutputStream.close();
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用Base64解密String，返回Object对象
     *
     * @param objectString 待解密的String
     * @return object      解密后的object
     */
    private static Object String2Object(String objectString) {
        byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            objectInputStream.close();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 使用SharedPreference保存对象
     *
     * @param fileKey    储存文件的key
     * @param key        储存对象的key
     * @param saveObject 储存的对象
     */
    public static void save(String fileKey, String key, Object saveObject) {
        SharedPreferences sharedPreferences = MyApplication.getInstance().getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String string = Object2String(saveObject);
        editor.putString(key, string);
        editor.commit();
    }

    /**
     * 获取SharedPreference保存的对象
     *
     * @param fileKey 储存文件的key
     * @param key     储存对象的key
     * @return object 返回根据key得到的对象
     */
    public static Object get(String fileKey, String key) {
        SharedPreferences sharedPreferences = MyApplication.getInstance().getApplicationContext().getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
        String string = sharedPreferences.getString(key, null);
        if (string != null) {
            Object object = String2Object(string);
            return object;
        } else {
            return null;
        }
    }

    /**
     * 清除通道列表缓存
     */
    public static void clearChannelInfo(){
        MyApplication.getInstance().getApplicationContext().getSharedPreferences(Constants.CHANNEL_INFO_SERIALIZE_FILE, Activity.MODE_PRIVATE).edit().clear().apply();
    }

//   反序列化 CarTempAttrList（缓存临时信息列表）
    public static CarTempAttrList getCarTempAttrList(){
        return (CarTempAttrList) get(Constants.CHANNEL_INFO_SERIALIZE_FILE,Constants.CAR_TEMP_ATTR_LIST) ;
    }


//    序列化 CarTempAttrList（缓存临时信息列表）
    public static void saveCarTempAttrList(Object saveObject){
        save(Constants.CHANNEL_INFO_SERIALIZE_FILE, Constants.CAR_TEMP_ATTR_LIST , saveObject);
    }


//    清空CarTempAttrList
    public static void clearCarTempAttrList(){
        save(Constants.CHANNEL_INFO_SERIALIZE_FILE, Constants.CAR_TEMP_ATTR_LIST , "");
    }


//    序列化车辆评估结果
    public static void saveEvaluateDetailResult(EvaluateDetailResult2 detailResult){
        save(Constants.CHANNEL_INFO_SERIALIZE_FILE, Constants.EVALUATE_DETAIL_RESULT , detailResult);
    }

//    反序列化车辆评估结果
    public static EvaluateDetailResult2 getEvaluateDetailResult(){
        return (EvaluateDetailResult2) get(Constants.CHANNEL_INFO_SERIALIZE_FILE,Constants.EVALUATE_DETAIL_RESULT);
    }

//   清空缓存评估记录数据
    public static void clearEvaluateDetailResult(){
        save(Constants.CHANNEL_INFO_SERIALIZE_FILE, Constants.EVALUATE_DETAIL_RESULT , null);
    }


    //   反序列化 CarBasicInfoList(缓存基础信息列表数据)
    public static CarBasicInfoList getCarBasicInfoList(){
        return (CarBasicInfoList) get(Constants.CHANNEL_INFO_SERIALIZE_FILE,Constants.CAR_BASIC_INFO_CACHE) ;
    }


    //    序列化 CarBasicInfoList(缓存基础信息列表数据)
    public static void saveCarBasicInfoList(Object saveObject){
        save(Constants.CHANNEL_INFO_SERIALIZE_FILE, Constants.CAR_BASIC_INFO_CACHE , saveObject);
    }


}
